package com.guragura.util;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;
import com.guragura.Guragura;
import com.guragura.screens.GameScreen;
import com.guragura.screens.GuraScreen;
import com.guragura.screens.HelpScreen;
import com.guragura.screens.MainMenuScreen;
import com.guragura.screens.SettingScreen;

/**
 * @author hartono
 * Menangani background music dari game ini
 */
public class GuraMusic {
	private Music mainMenuBgm;
	private Music gameBgm;
	private Music gameCriticalBgm;
	private Guragura guragura;
	private AssetManager assetManager;

	private Music napasBgm;
	private Music gempa1Bgm;
	private Music gempa2Bgm;

	public GuraMusic(Guragura guragura) {
		this.guragura = guragura;
		assetManager = guragura.assetManager;

		mainMenuBgm = assetManager.get("bgm/mainmenu.mp3", Music.class);
		gameBgm = assetManager.get("bgm/play1.mp3", Music.class);
		gameCriticalBgm = assetManager.get("bgm/play2.mp3", Music.class);
		napasBgm = assetManager.get("bgm/napas.mp3", Music.class);
		gempa1Bgm = assetManager.get("bgm/gempa1.mp3", Music.class);
		gempa2Bgm = assetManager.get("bgm/gempa2.mp3", Music.class);

		gameBgm.setLooping(true);
		gameCriticalBgm.setLooping(true);
		mainMenuBgm.setLooping(true);
		napasBgm.setLooping(true);
		gempa1Bgm.setLooping(true);
		gempa2Bgm.setLooping(true);
	}

	public void playMainMenuBgm() {
		if (GuraSettings.sound)
			if (!mainMenuBgm.isPlaying()) {
				mainMenuBgm.play();
			}
	}

	public void stopMainMenuBgm() {
		if (mainMenuBgm.isPlaying()) {
			mainMenuBgm.stop();
		}
	}	
	
	public void playGameBgm() {
		if (!gameBgm.isPlaying()) {
			napasBgm.play();
			gameBgm.play();
			gempa1Bgm.play();
		}
	}

	public void playGameCriticalBgm() {
		if (gameBgm.isPlaying()) {
			stopGameBgm();
			gameCriticalBgm.play();
			napasBgm.play();
			gempa2Bgm.play();
		}

	}

	public void stopGameBgm() {
		if (gameBgm.isPlaying()) {
			gameBgm.stop();
		}
		if (gempa1Bgm.isPlaying()) {
			gempa1Bgm.stop();
		}
		if (napasBgm.isPlaying()) {
			napasBgm.stop();

		}

	}

	public void stopGameCriticalBgm() {
		if (GuraSettings.sound) {
			if (gameCriticalBgm.isPlaying()) {
				gameCriticalBgm.stop();
			}
			if (napasBgm.isPlaying()) {
				napasBgm.stop();
			}
			if (gempa2Bgm.isPlaying()) {
				gempa2Bgm.stop();
			}
		}
	}

	/**
	 * Programmer cukup memanggil method ini pada point2 yang dibutuhkan.
	 * cukup mengecek state dari {@link Guragura#gameState} untuk mengetahui user sedang berada pada
	 * screen yang mana.
	 * 
	 * method ini dipanggil biasanya pada {@link GuraScreen#show()} , {@link GuraScreen#hide()}, {@link GuraScreen#resume()}, {@link GuraScreen#pause()}
	 * 
	 */
	public void play() {
		if (GuraSettings.sound) {
			if (guragura.gameState == Guragura.MAIN_MENU) {
				playMainMenuBgm();
			} else if (guragura.gameState == Guragura.GAME) {
				stopMainMenuBgm();
				playGameBgm();
			} else if (guragura.gameState == Guragura.SETTING_SCREEN) {
				if (guragura.sScreens.get(guragura.sScreens.size() - 2) instanceof MainMenuScreen) {
					playMainMenuBgm();
				} else if (guragura.sScreens.get(guragura.sScreens.size() - 2) instanceof GameScreen) {
					playGameBgm();
				}
			}
		} else {
			stopGameBgm();
			stopGameCriticalBgm();
			stopMainMenuBgm();
		}
	}

	/**
	 *  sama seperti {@link GuraMusic#play()} , namun method ini digunakan untuk menghentikan background music
	 *  
	 */
	public void stop() {
		if (GuraSettings.sound) {
			if (guragura.gameState == Guragura.GAME) {
				if (guragura.sScreens.peek() instanceof SettingScreen) {

				} else if (guragura.sScreens.peek() instanceof HelpScreen) {

				} else {
					stopGameBgm();
					stopGameCriticalBgm();
				}

			}
		} else {
			stopMainMenuBgm();
			stopGameBgm();
			stopGameCriticalBgm();
		}
	}

	/**
	 * untuk mematikan / menyalakan music ketika user menekan tombol sound on / off pada
	 * setting screen ({@link SettingScreen})
	 */
	public void toggleMusic() {		
		if (GuraSettings.sound) {
			play();
		} else {
			stop();
		}
	}

}
